// import { previewImage } from "@/components"
// import useStore from "@/store"

// 获取本地静态资源文件, 注意：vite 6 版本会有问题
export const getAssetsFile = (url: string) => new URL(`../assets/${url}`, import.meta.url).href

/**
 * 深拷贝
 * @param {obj}
 * @returns {Object}
 */
export const deepClone = <T>(obj: T): T => {
  // 处理null、undefined和其他非对象类型
  if (obj === null || typeof obj !== "object") {
    return obj
  }

  // 处理日期对象
  if (obj instanceof Date) {
    return new Date(obj.getTime()) as T
  }

  // 处理正则对象
  if (obj instanceof RegExp) {
    return new RegExp(obj) as T
  }

  // 处理数组
  if (Array.isArray(obj)) {
    return obj.map((item) => deepClone(item)) as unknown as T
  }

  // 处理对象
  const cloneObj = {} as T
  for (const key in obj) {
    if (obj.hasOwnProperty(key)) {
      cloneObj[key] = deepClone(obj[key])
    }
  }
  return cloneObj
}

// 预览多张文件
export const previewFile = (url: string, urls: { url: string }[] = []) => {
  let files = urls.map(item => item.url)
  let imgs = files.filter((item) => isImage(item))
  let index = imgs.findIndex((item) => item === url)
  if (index !== -1) {
    // previewImage({ urlList: imgs, initialIndex: index })
    window.open(url, '_blank')
  } else {
    if (isImage(url)) {
      // previewImage({ urlList: [url] })
      window.open(url, '_blank')
    } else {
      window.open(url, '_blank')
    }
  }
}

/**
 * @description 判断是否链接为图片
 * @param url 链接
 * @returns 是否为链接
 */
export const isImage = (url: string): boolean => /\.(jpg|jpeg|png|gif|webp|svg)$/i.test(url)

/**
 * @description 获取链接格式
 * @param {string} url 链接
 * @returns 链接格式
 */
export const format = (url: string): string => url.slice(url.lastIndexOf(".") + 1)

/**
 * @description 获取字典项的 label
 * @param {number | string} val 字典项值
 * @param {string} key 字典项 key
 * @returns 字典项的 label
 */
export const getDictLabel = (val: number | string, key: string): string => {
  // useStore().dict.dictMap[key]?.find((d: Dict) => String(d.value) === String(val))?.label ?? ""
  return ""
}

// base64转file格式
export const base64ToFile = (data: string) => {
  let base = window.atob(data.substring(data.indexOf(",") + 1))
  let length = base.length
  let url = new Uint8Array(length)
  while (length--) {
    url[length] = base.charCodeAt(length)
  }
  let file = new File([url], `${new Date().getTime()}.jpg`, {
    type: "image/jpg",
  })
  return file
}

// 尺寸转换
export const formatSize = (size: number) => {
  if (size > 1024) {
    if (size / 1024 > 1024) {
      return `${(size / 1024 / 1024).toFixed(2)} MB`
    }
    return `${(size / 1024).toFixed(2)} KB`
  }
  return `${size.toFixed(2)} B`
}

// 防抖
export const debounce = <T extends (...args: any[]) => void>(fun: T, delay: number = 300): T => {
  let timer: NodeJS.Timeout | null = null
  return function (this: any, ...args: Parameters<T>) {
    if (timer !== null) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      fun.apply(this, args)
      timer = null
    }, delay)
  } as T
}

// 节流
export const throttle = <T extends (...args: any[]) => void>(fun: T, time: number = 300): T => {
  let t1 = 0
  return function (this: any, ...args: Parameters<T>) {
    const t2 = new Date().getTime()
    if (t2 - t1 > time) {
      fun.apply(this, args)
      t1 = t2
    }
  } as T
}


// 清除富文本标签
export const cleanHtmlTag = (html: string): string => {
  const tagStripper = /<\/?[^>]+(>|$)/g
  let textWithoutTags = html.replace(tagStripper, '')
  const entityMap: { [key: string]: string } = {
    '&nbsp;': ' ',
    '&amp;': '&',
    '&quot;': '"',
    '&lt;': '<',
    '&gt;': '>',
  }
  const entityStripper = /&[a-zA-Z0-9#]+;/g
  return textWithoutTags.replace(entityStripper, (entity) => {
    return entityMap[entity] || ''
  })
}